// 回复业务代码
const {resolve} = require("path")
exports.reply = async (ctx, next) => {
    const message = ctx.weixin

    let mp = require("./index")
    let client = mp.getWechat()

    if (message.MsgType === 'text'){
        let content = message.Content
        let reply = 'oh, 你说的 ' + content + ' 太复杂了，无法解析'
        // let reply = ''
        // content = content.replace("吗", "")
        // content = content.replace("你是谁", "我是小可爱")
        // let wo = content.indexOf("我")
        // let ni = content.indexOf("你")
        // content = content.replace(/^你/, "我")
        // content = content.replace(/我$/, "你")

        // content = content.replace("是不是", "是")
        // content = content.replace("？", "！")
        // reply = content
        if(content === '1'){
            reply = '天下第一'
        } else if(content === '2'){
            reply = '天下第二'
        } else if(content === '3'){
            reply = '天下第三'
        } else if(content === 'imooc'){
            reply = '慕课实战课'
        } else if(content === '4'){
            let data = await client.handle('uploadMaterial', 'image', resolve(__dirname, '../2.jpg'))
            reply = {
                type: 'image',
                mediaId: data.media_id
            }
        } else if(content === '5'){
            let data = await client.handle('uploadMaterial', 'video', resolve(__dirname, '../6.mp4'))
            reply = {
                type: 'video',
                title: '回复的视频标题',
                description: '回复的视频描述',
                mediaId: data.media_id
            }
        } else if(content === '6'){
            let data = await client.handle('uploadMaterial', 'video', resolve(__dirname, '../6.mp4'), {
                type: 'video',
                description: '{"title":"这个地方很棒"}',
                introduction: '我是introduction'
            })


            reply = {
                type: 'video',
                title: '回复的视频标题 2',
                description: '回复的视频描述',
                mediaId: data.media_id
            }
        } else if(content === '7'){
            let data = await client.handle('uploadMaterial', 'image', resolve(__dirname, '../2.jpg'), {
                type: 'image'
            })
            reply = {
                type: 'image',
                mediaId: data.media_id
            }
        } else if(content === '8'){
            let data = await client.handle('uploadMaterial', 'image', resolve(__dirname, '../2.jpg'), {
                type: 'image'
            })

            let media = {
                articles: [
                    {
                        title: '这是服务端上传的图文1',
                        thumb_media_id: picData.media_id,
                        author: 'bobo',
                        digest: '没有摘要',
                        show_cover_pic: 1,
                        content: '点击去往慕课网',
                        content_source_url: 'http://coding.imooc.com/'
                    },
                    {
                        title: '这是服务端上传的图文2',
                        thumb_media_id: picData.media_id,
                        author: 'bobo',
                        digest: '没有摘要2',
                        show_cover_pic: 1,
                        content: '点击去往github',
                        content_source_url: 'http://github.com/'
                    }
                ]
            }

            reply = {
                type: 'image',
                mediaId: data.media_id
            }
        }


        ctx.body = reply
    }

    await next()
}